var setting = {
		check: {
			enable: false,
		},
		async: {
			enable : true,
			url : ctx + "/tree/organ",
			autoParam : [ "id"],
			dataFilter: filter,
			type : "get"
		},
		data: {
			simpleData: {
				enable: true,
				pIdKey: "pid",
			},
			key:{
				name:'name'
			}
		},
		view: {
			expandSpeed: ""
		},
		callback: {
			beforeAsync : beforeAsync,
			onAsyncSuccess : onAsyncSuccess,
			onAsyncError : onAsyncError
		}
	};
$(function(){
	
	getTree();
	
	/**
	 * 绑定查询事件
	 */
	$('#organtreeform #search_link').linkbutton({
		onClick:function(){
			findByName();
		}
	});
	
	/**
	 * 绑定重置事件
	 */
	$('#organtreeform #reset_link').linkbutton({
		onClick:function(){
			$("#organtreeform #oname").textbox("setValue","");
			reset();
		}
	});
});

var curStatus = "init", curAsyncCount = 0, asyncForAll = false, goAsync = false;

/**
 * 获取机构树
 */
function getTree(){
	findByPid(ctx + "/tree/organ" ,{id:$("#index #organId").val()},initOrganTree);
}

/**
 * 获取机构树对象
 * @returns
 */
function getTreeObj(){
	return $.fn.zTree.getZTreeObj("organTree");
}

/**
 * 初始化机构树
 * @param data
 */
function initOrganTree(data){
	var pData = {id:$('#index #organId').val(),code:$('#index #organCode').val(),name:$('#index #organName').val(),open:true,isParent:true,pid:0}
	if(data.length == 0){
		pData.isParent = false;
	}
	
	data.push(pData);
	
	$.fn.zTree.init($("#organTree"), setting, data);
	
	var tree = $.fn.zTree.getZTreeObj("organTree");
	var node = tree.getNodeByParam("id", $('#index #organId').val());
	tree.selectNode(node);
	if(tree.setting.callback.onClick != null){
		tree.setting.callback.onClick(null, tree.setting.treeId, node);//调用事件
	}
	
	if(typeof(initSuccessCallBack)=="function" && $("#rt").val() == 'rt'){
		initSuccessCallBack(data);
	}
}